package model.interactionhandler;

import model.entity.Avatar;
import model.entity.BasicEntity;
import model.entity.stats.StatModifier;
import model.entity.stats.StatType;
import model.item.Item;
import model.item.TakeableItem;
import model.map.World;

import java.util.Map;
import java.util.HashMap;

/**
 * this class is used for handling the buying interaction
 * (an avatar is buying from a merchant)
 * @author Vincent
 *
 */
public class BuyHandler extends InteractionHandler{
	
	public BuyHandler(String theName) {
		super(theName);
	}

	/**
	 * returns the sellers inventory with the prices attached
	 * @param bEnt
	 * @return
	 */
	public Map<Item, Integer> getInventoryWithPrices(BasicEntity bEnt){
		Map<Item, Integer> outMap = new HashMap<Item, Integer>();
		double rating = World.getInstance().getPlayer().getStatVal(StatType.BARGIN);
		double pricePercent = 1.0;
		if( rating < 5 ){
			pricePercent = 0.95;
		}
		else if( rating > 20){
			pricePercent = 0.80;
		}
		else if(rating > 50){
			pricePercent = 0.70;
		}
		else 
			pricePercent = 0.50;
		for (TakeableItem i : bEnt.getInventoryItems()){
			outMap.put(i, ((int)(i.getPrice()* pricePercent)));
		}
		return outMap;
	}
	
	/**
	 * checks if the avatar has enough money to purchase the
	 * item that was passed in
	 * @param av
	 * @param it
	 * @return
	 */
	public boolean canBuy(Avatar av, TakeableItem it){
		return av.getStatVal(StatType.MONEY) >= (double)it.getPrice();
	}
	
	/**
	 * does the actual transaction of paying and exchanging the item
	 * @param av
	 * @param ent
	 * @param it
	 */
	public void buy(Avatar av, BasicEntity ent, TakeableItem it){
		// Pay the money
		double cost = (double)it.getPrice();
		av.addConstantModifier(StatType.MONEY, -cost);
		ent.addConstantModifier(StatType.MONEY, cost);
		
		// Give up the item
		ent.removeItem(it);
		
		// Acquire the Item
		av.addItem(it);
	}
}